#include <iostream>
#include <cstring>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>

#define MAXN 5005
using namespace std;

int dfn[MAXN], low[MAXN], color[MAXN], vis[MAXN];
int sum;
int n, m;
stack<int> s;
vector<int> G[MAXN], GS[MAXN];
int deep;

void clean(stack<int> &st) {
	stack<int> empty;
	swap(st, empty);
}

void tarjan(int u) {
	deep++;
	dfn[u] = deep;
	low[u] = deep;
	s.push(u);
	for (unsigned i = 0; i < G[u].size(); i++) {
		int v = G[u][i];
		if (!dfn[v]) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
		} else if (!color[v]) {
			low[u] = min(low[u], low[v]);
		}
	}
	
	if (dfn[u] == low[u]) {
		sum++;
		color[u] = sum;
		while (s.top() != u) {
			color[s.top()] = sum;
			s.pop();
		}
		s.pop();
	}
	/*
	if (dfn[u] == low[u]) {
		color[u] = u;
		while (s.top() != u) {
			color[s.top()] = u;
			s.pop();
		}
		s.pop();
	}*/
}

bool TopSort() {
	queue<int> q;
	for (int i = 1; i <= sum; ++i) {
		if(vis[i] == 0) q.push(i);
	}

	while(!q.empty()) {
		if(q.size() > 1) return 0;

		int t = q.front();
		q.pop();
		for(unsigned i = 0; i < GS[t].size(); ++i) {
			--vis[GS[t][i]];
			if(vis[GS[t][i]] == 0) {
				q.push(GS[t][i]);
			}
		}
	}
	
	return 1;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	int T;
	cin >> T;
	while (T--) {
		int x, y;
		cin >> n >> m;
		for (int i = 1; i <= m; ++i) {
			cin >> x >> y;
			G[x].push_back(y);
		}

		for (int i = 1; i <= n; ++i) {
			if (!dfn[i]) tarjan(i);
		}

		for (int i = 1; i <= n; ++i) {
			for (unsigned j = 0; j < G[i].size(); ++j) {
				int v = G[i][j];
				if (color[i] != color[v]) {
					GS[color[i]].push_back(color[v]);
					++vis[color[v]];
				}
			}
		}

		if (TopSort()) {
			cout << "Yes" << endl;
		} else {
			cout << "No" << endl;
		}

		for (int i = 0; i <= n; ++i) {
			G[i].clear();
			GS[i].clear();
		}
		sum = 0;
		memset(dfn, 0, sizeof(dfn));
		memset(color, 0, sizeof(color));
		memset(low, 0, sizeof(low));
		deep = 0;
	}
	return 0;
}
